/**************************************************************************(C)*
 *
 *  This file is part of the GreenHills project
 *  Copyright (C) 2009  Tieto Corporation
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  Not for commmercial use. Only with Tieto permission.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************(C)*/

#include "../updateobjectevent.h"
#include "util/binstream.h"

#include <string.h>

using namespace greenhills::core;
using namespace greenhills::event;
using namespace greenhills::util;

REGISTER_EVENT( CEvent::UpdateObjectRequest, CUpdateObjectRequestEvent);
REGISTER_EVENT( CEvent::UpdateObjectResponse, CUpdateObjectResponseEvent);
REGISTER_EVENT( CEvent::ObjectUpdatedEvent, CItemUpdatedEvent);

CUpdateObjectRequestEvent::CUpdateObjectRequestEvent()
    :CActionEvent(), mData(0), mLength(0)
{
}

CUpdateObjectRequestEvent::CUpdateObjectRequestEvent(
        const CRoomId &pRoomId, const CItemId &pItemId,
        const unsigned char *pData, unsigned int pLength)
    :CActionEvent()
{
    mRoomId = pRoomId;
    mItemId = pItemId;
    mLength = pLength;
    mData = new unsigned char[pLength];
    memcpy(mData, pData, pLength);
}

CUpdateObjectRequestEvent::~CUpdateObjectRequestEvent()
{
    if (mData)
        delete[] mData;
}

obinstream& CUpdateObjectRequestEvent::operator<<(obinstream &pStream) const
{
    CActionEvent::operator <<(pStream);

    pStream << mRoomId;
    pStream << mItemId;
    pStream << mLength;
    pStream.write((char*)mData, mLength);

    return pStream;
}

ibinstream& CUpdateObjectRequestEvent::operator>>(ibinstream &pStream)
{
    CActionEvent::operator >>(pStream);

    if (mData)
        delete[] mData;

    pStream >> mRoomId;
    pStream >> mItemId;
    pStream >> mLength;
    mData = new unsigned char[mLength];
    pStream.read((char*)mData, mLength);

    return pStream;
}


CUpdateObjectResponseEvent::CUpdateObjectResponseEvent()
    :CActionEvent()
{
}

CUpdateObjectResponseEvent::CUpdateObjectResponseEvent(
        unsigned int pTransactionId, bool pSucceeded)
    :CActionEvent(pTransactionId), mSucceeded(pSucceeded)
{
}

obinstream& CUpdateObjectResponseEvent::operator<<(obinstream &pStream) const
{
    CActionEvent::operator <<(pStream);

    pStream << mSucceeded;
    return pStream;
}

ibinstream& CUpdateObjectResponseEvent::operator>>(ibinstream &pStream)
{
    CActionEvent::operator >>(pStream);

    pStream >> mSucceeded;
    return pStream;
}




CItemUpdatedEvent::CItemUpdatedEvent()
    :CEvent(), mData(0), mLength(0)
{
}

CItemUpdatedEvent::CItemUpdatedEvent(
        const CRoomId &pRoomId, const CItemId &pItemId,
        const unsigned char *pData, unsigned int pLength)
    :CEvent()
{
    mRoomId = pRoomId;
    mItemId = pItemId;
    mLength = pLength;
    mData = new unsigned char[pLength];
    memcpy(mData, pData, pLength);
}

CItemUpdatedEvent::~CItemUpdatedEvent()
{
    if (mData)
        delete[] mData;
}

obinstream& CItemUpdatedEvent::operator<<(obinstream &pStream) const
{
    CEvent::operator <<(pStream);

    pStream << mRoomId;
    pStream << mItemId;
    pStream << mLength;
    pStream.write((char*)mData, mLength);

    return pStream;
}

ibinstream& CItemUpdatedEvent::operator>>(ibinstream &pStream)
{
    CEvent::operator >>(pStream);

    if (mData)
        delete[] mData;

    pStream >> mRoomId;
    pStream >> mItemId;
    pStream >> mLength;
    mData = new unsigned char[mLength];
    pStream.read((char*)mData, mLength);

    return pStream;
}
